package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.management.aop.annotation.LogAnno;
import com.qk.management.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activities")
@Slf4j
public class ActivityController {
    @Autowired
    private ActivityService activitieService;

    @GetMapping
    public Result page(@RequestParam(name="page",required = true,defaultValue = "1") Integer page,
                                     @RequestParam(name="pageSize",required = true,defaultValue = "10") Integer pageSize,
                                     @RequestParam(name="channel",required = false) String channel,
                                     @RequestParam(name="type",required = false) Integer type,
                                     @RequestParam(name="status",required = false) Integer status
    ){
        log.info("获取活动分页列表: page= {},pageSize={},channel={},type={},status={}",page,pageSize,channel,type,status);
        PageResult<Activity> pageResult = activitieService.page(page, pageSize, channel, type, status);
        return Result.success(pageResult);
    }

    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        log.info("删除活动: {}",id);
        activitieService.deleteById(id);
        return Result.success();
    }

    @LogAnno
    @PostMapping
    public Result add(@RequestBody Activity activity){
        log.info("添加活动: {}",activity);
        activitieService.add(activity);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        log.info("获取活动: {}",id);
        Activity activity = activitieService.getById(id);
        return Result.success(activity);
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody Activity activity){
        log.info("修改活动: {}",activity);
        activitieService.update(activity);
        return Result.success();
    }

    @GetMapping("/type/{type}")
    public Result findByType(@PathVariable("type") Integer type){
        log.info("获取活动: {}",type);
        List<Activity> activities = activitieService.findByType(type);
        return Result.success(activities);
    }
}
